/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package net.wofly.right.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import net.wofly.right.domain.Organization;

public interface OrganizationRepository
        extends PagingAndSortingRepository<Organization, String>, JpaSpecificationExecutor<Organization> {
    
    List<Organization> findByStatus(Integer status);
    
    Organization findByOrganizationCode(String organizationCode);
    
    List<Organization> findByOrganizationName(String organizationName);
    
    @Query("select max(organizationCode) from Organization o where o.organizationCode > '100200100'")
    Integer findMaxOrganizationCode();
    
    @Query("select o from Organization o where o.organizationType > ?1 and status=?2")
    List<Organization> findByOrganizationType(String organizationType ,Integer status) ;
    
    List<Organization> findByWarehouseFlagAndStatus(Integer warehouseFlag ,Integer status) ;
    
    List<Organization> findByWarehouseFlagAndStatusAndOrganizationCodeNot(Integer warehouseFlag ,Integer status,String organizationCode) ;
    
    List<Organization> findByOrganizationCodeStartingWithAndOrganizationCodeNot(String organizationCode1,String organizationCode2);
    
    @Query("select o from Organization o ,User u where o.organizationID = u.userID and u.followUserID = ?1 "
    		+ " and o.organizationName like %?2% and o.linkman like %?3% and o.mobile like %?4%")
    Page<Organization> findMyCustomer(String salesUserID, String customName, String linkMan, String mobile, Pageable pageable);
    
    @Query("select o from Organization o ,User u where o.organizationID = u.userID and u.followUserID = ?1 and o.organizationType like %?7% "
            + " and o.organizationName like %?2% and o.organizationNamePinYin like %?3%  and o.linkman like %?4%  and o.linkmanPinYin like %?5% and o.mobile like %?6% and o.organizationCode > '100200100'")
    Page<Organization> findMyCustomer(String salesUserID, String customName,String customerNamePinYin,  String linkMan,String linkManPinYin, String mobile, String organizationType, Pageable pageable);
   
    @Query("select o from Organization o where o.organizationName like %?1% and o.linkman like %?2% and o.mobile like %?3%")
    Page<Organization> findAllCustomer(String customName, String linkMan, String mobile, Pageable pageable);
    
    @Query("select o from Organization o where o.organizationName like %?1% and o.organizationNamePinYin like %?2% and o.linkman like %?3% and o.linkmanPinYin like %?4%  and o.mobile like %?5%")
    Page<Organization> findAllCustomer(String customName,String customerNamePinYin, String linkMan, String linkManPinYin,String mobile, Pageable pageable);
    
    @Query("select o from Organization o where o.organizationCode like ?1% and o.organizationCode != ?2 and o.status =?3 ")
    Page<Organization> findSubOrgsByOrgCode(String orgCode1, String orgCode2,Integer status,Pageable pageable);
    
    @Query("select o from Organization o where o.organizationCode like ?1% and o.status =?2 ")
    List<Organization> findOrgsByOrgCode(String orgCode1,Integer status);
}